package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    //注入本地dao
    @Autowired
    private PermissionDao permissionDao;

    /*
        查询所有权限
     */
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        //分页助手
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = permissionDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Permission permission) {
        //新增
        permissionDao.add(permission);
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    @Override
    public void delete(Integer id) {
        //查询当前权限是否和角色关联
        long count=permissionDao.findCountByPermissionId(id);
        if(count>0){
            //当前权限被引用,不能删除
            throw new RuntimeException("当前权限被引用,不能删除");
        }
        permissionDao.deleteById(id);
    }
}
